package nciae.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import nciae.db.common.CommonResult;
import nciae.db.entity.User;
import nciae.db.mapper.UserMapper;
import nciae.db.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.invoke.CallSite;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public CommonResult add(User user) {
        CommonResult rs = new CommonResult();
        try {
            int k = userMapper.insert(user);

            if (k > 0) {
                rs.setCode(0);
                rs.setMsg("添加成功");
            } else {
                rs.setCode(-1);
                rs.setMsg("插入失败");
            }
            return rs;
        }catch (Exception e){
            rs.setCode(-1);
            rs.setMsg(e.getMessage());
            return rs;
        }
    }

    @Override
    public CommonResult edit(User user) {
        CommonResult rs = new CommonResult();
        try {


            int k = userMapper.updateById(user);
            if (k > 0) {
                rs.setCode(0);
                rs.setMsg("修改成功");
            } else {
                rs.setCode(-1);
                rs.setMsg("修改失败");
            }
            return rs;
        }catch (Exception e){
            rs.setCode(-1);
            rs.setMsg(e.getMessage());
            return rs;
        }
    }

    @Override
    public CommonResult list(String userName, String realName) {
        CommonResult rs = new CommonResult();
        try {
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            if (userName != null && !userName.isEmpty()) {
                queryWrapper.eq("user_name", userName);
            }
            if (realName != null && !realName.isEmpty()) {
                queryWrapper.eq("real_name", realName);
            }
            List<User> data = userMapper.selectList(queryWrapper);
            rs.setCode(0);
            rs.setMsg("查询成功");
            rs.setData(data);
            return rs;
        }catch (Exception e){
            rs.setCode(-1);
            rs.setMsg(e.getMessage());
            return rs;
        }
    }

    @Override
    public CommonResult listByPage(String userName, String realName, Integer page, Integer size) {
        CommonResult rs = new CommonResult();
        try{
            Page<User> pg = new Page<>(page,size);
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            if (userName != null && !userName.isEmpty()) {
                queryWrapper.eq("user_name", userName);
            }
            if (realName != null && !realName.isEmpty()) {
                queryWrapper.eq("real_name", realName);
            }
            userMapper.selectPage(pg,queryWrapper);
            List<User> data = pg.getRecords();
            long count = pg.getTotal();

            rs.setCode(0);
            rs.setMsg("查询成功");
            rs.setData(data);
            rs.setCount(count);
            return rs;

        }catch (Exception e){
            rs.setCode(-1);
            rs.setMsg(e.getMessage());
            return rs;
        }
    }


    @Override
    public CommonResult del(Integer id) {
        CommonResult rs = new CommonResult();
        try{
            int k = userMapper.deleteById(id);
            if(k>0){
                rs.setCode(0);
                rs.setMsg("删除成功");
                return rs;
            }else{
                rs.setCode(-1);
                rs.setMsg("删除失败");
                return rs;
            }
        }catch (Exception e){
            rs.setCode(-1);
            rs.setMsg(e.getMessage());
            return rs;
        }
    }

    @Override
    public User getUserById(Integer id) {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public User login(User user) {
        return userMapper.getByUsernameAndPassword(user);
    }
}
